package 面试真题;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Scanner;

class V {
    public int x;
    public int y;
}

public class Ali401 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        while (n-- > 0) {
            int m = scanner.nextInt();
            ArrayList<V> vs = new ArrayList<>();
            for (int i = 0; i < m; i++) {
                if(!vs.isEmpty()){
                    V v = vs.get(vs.size()-1);
                    int a = scanner.nextInt();
                    int b = scanner.nextInt();
                    if(v.x != a){
                        vs.add(new V());
                        vs.get(i).x = a;
                        vs.get(i).y = b;
                    }
                }else{
                    int a = scanner.nextInt();
                    int b = scanner.nextInt();
                    vs.add(new V());
                    vs.get(i).x = a;
                    vs.get(i).y = b;
                }
            }
            vs.sort(new Comparator<V>() {
                @Override
                public int compare(V o1, V o2) {
                    return o2.x - o1.x;
                }
            });
            int[][] dp = new int[m][m];
            for (int i = 0; i < m; i++) {
                dp[i][i] = 1;
            }
            for (int len = 3; len <= vs.size(); len++) {
                for (int i = 0; i + len - 1 < vs.size(); i++) {
                    if (vs.get(i) == vs.get(i + 1)) {
                        dp[i][i + len - 1] = 2;
                    } else {
                        if (vs.get(i).y < vs.get(i + len - 1).y) {
                            dp[i][i + len - 1] = dp[i + 1][i + len - 2] + 2;
                        } else {
                            dp[i][i + len - 1] = dp[i + 1][i + len - 2];
                        }
                    }
                }
            }
            System.out.println(dp[0][vs.size() - 1]);
        }
    }
}
